import 'package:ai/AppRouter.gr.dart';
import 'package:ai/entity/device_info.dart';
import 'package:ai/page/mine/utils.dart';
import 'package:ai/response/MemberInfoResponse.dart';
import 'package:ai/service/MineService.dart';
import 'package:ai/util/CommonUtils.dart';
import 'package:ai/util/SharedPreferencesUtil.dart';
import 'package:ai/util/ToastUtils.dart';
import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';

import '../../config/Config.dart';

@RoutePage()
class MinePage extends StatefulWidget {
  const MinePage({super.key});

  @override
  State<MinePage> createState() => _MinePageState();
}

class _MinePageState extends State<MinePage> {
  final MineService mineService = MineService();

  late String code = "";

  late String token = "";

  late Member _member = Member.init();

  @override
  void initState() {
    super.initState();
    print("mine:${DeviceInfo.height}");
    var s = SharedPreferencesUtil().getString("token") ?? "";
    setState(() {
      token = s;
    });
    if (s.isNotEmpty) {
      mineService.currentUser().then((member) {
        setState(() {
          _member = member;
          token = member.username.isEmpty ? "" : s;
        });
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.symmetric(
        horizontal: 16,
      ),
      decoration: const BoxDecoration(
        image: DecorationImage(
          alignment: Alignment.topCenter,
          image: AssetImage("asset/images/bg_mine.png"),
          fit: BoxFit.fitWidth,
        ),
      ),
      child: ListView(
        children: [
          const SizedBox(
            height: 32,
          ),
          Row(
            children: [
              CommonUtils.getImageAsset("icon_head_gray", width: 64),
              const SizedBox(
                width: 16,
              ),
              token.isEmpty
                  ? GestureDetector(
                      onTap: () {
                        CommonUtils.go(context, const LoginRoute());
                      },
                      child: const Text(
                        "请登录",
                        style: TextStyle(
                          fontSize: 16,
                        ),
                      ),
                    )
                  : Column(
                      mainAxisAlignment: MainAxisAlignment.start,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          _member.username,
                          style: const TextStyle(
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                        Text(
                          "ID:${_member.mobile}",
                          style: TextStyle(
                            color: Config.color76777c,
                          ),
                        ),
                        Text(
                          "会员到期时间:${_member.expiredDate}",
                          style: TextStyle(
                            color: Config.color76777c,
                          ),
                        ),
                      ],
                    )
            ],
          ),
          const SizedBox(
            height: 32,
          ),
          CommonUtils.getImageAsset("show_charge_tips"),
          const SizedBox(
            height: 8,
          ),
          CommonUtils.getImageAsset("bg_ai_create"),
          const SizedBox(
            height: 16,
          ),
          const Text(
            "常用功能",
            style: TextStyle(
              fontSize: 20,
            ),
          ),
          const SizedBox(
            height: 16,
          ),
          MineUtils.getList("会员充值", "icon_mine_charge", () {
            CommonUtils.go(context, const RechargeRoute());
          }),
          MineUtils.getList("消费记录", "icon_mine_consume", () {
            CommonUtils.go(context, const RecordRoute());
          }),
          MineUtils.getList("联系客服", "icon_mine_kefu", () {
            CommonUtils.go(context, const RecordRoute());
          }),
          MineUtils.getList("兑换码", "icon_mine_dhm", () {
            openDialog();
          }),
          MineUtils.getList("设置", "icon_mine_set", () {
            CommonUtils.go(context, const SettingRoute());
          }),
          MineUtils.getList("举报", "icon_jubao", () {}),
        ],
      ),
    );
  }

  void openDialog() {
    CommonUtils.openModalBottomSheet(
      context,
      child: Container(
        height: 400,
        child: Column(
          children: [
            const SizedBox(
              height: 16,
            ),
            Row(
              children: [
                const SizedBox(
                  width: 48,
                ),
                Expanded(
                    child: Text(
                  "兑换码",
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    fontSize: 18,
                    color: Config.color34,
                  ),
                )),
                IconButton(
                  onPressed: () {
                    Navigator.pop(context);
                  },
                  icon: Icon(
                    Icons.close,
                    color: Config.color3,
                  ),
                ),
                const SizedBox(
                  width: 16,
                )
              ],
            ),
            const SizedBox(
              height: 16,
            ),
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 24),
              child: TextField(
                onChanged: (value) {
                  setState(() {
                    code = value;
                  });
                },
                cursorColor: Config.primaryColor,
                decoration: InputDecoration(
                  fillColor: Config.colorF0,
                  filled: true,
                  hintText: "请输入兑换号码",
                  hintStyle: TextStyle(
                    color: Config.color9a,
                  ),
                  enabledBorder: OutlineInputBorder(
                    borderSide: BorderSide(
                      color: Config.color69f,
                      width: 1,
                    ),
                    borderRadius: BorderRadius.circular(8),
                  ),
                  focusedBorder: OutlineInputBorder(
                    borderSide: BorderSide(
                      color: Config.colorE5,
                      width: 2,
                    ),
                    borderRadius: BorderRadius.circular(8),
                  ),
                ),
              ),
            ),
            Container(
              padding: const EdgeInsets.all(24),
              width: double.infinity,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    "兑换规则：",
                    style: TextStyle(
                      fontSize: 18,
                      fontWeight: FontWeight.bold,
                      color: Config.color34,
                    ),
                  ),
                  const SizedBox(
                    height: 8,
                  ),
                  Text(
                    "1.兑换码号不可重复使用",
                    style: TextStyle(
                      color: Config.color9a,
                    ),
                  ),
                  const SizedBox(
                    height: 4,
                  ),
                  Text(
                    "2.兑换码仅限兑换会员，不可换现金，请妥善保管",
                    style: TextStyle(
                      color: Config.color9a,
                    ),
                  ),
                  const SizedBox(
                    height: 4,
                  ),
                  Text(
                    "3.兑换后，如遇到未到账的情况请联系客服",
                    style: TextStyle(
                      color: Config.color9a,
                    ),
                  ),
                ],
              ),
            ),
            const SizedBox(
              height: 16,
            ),
            Container(
              width: 400,
              child: Expanded(
                child: SizedBox(
                  width: double.infinity,
                  child: FilledButton(
                    style: FilledButton.styleFrom(
                      backgroundColor: Config.primaryColor,
                    ),
                    onPressed: () {
                      ///
                      if (code.isNotEmpty) {
                      } else {
                        ToastUtils.info(context, "请输入兑换码");
                      }

                      Navigator.pop(context);
                    },
                    child: const Text('立即兑换'),
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
